/*
 * relative     : \src\stores\modules\commonDictionary.ts
 * Author       : 郭鹏超
 * Date         : 2025-02-11 08:46
 * LastEditors  : SJZ
 * LastEditTime : 2025-04-04 00:57
 * Description  :系统字典公共属性
 * CodeIterationRecord:
 */
const useCommonDictionaryStore = defineStore({
  id: "commonDictionary",
  state: (): SystemDictionaryView => {
    return {
      roles: [] as SelectOptionsView[],
      avoidHolidaySettingList: [] as SelectOptionsView[],
      frequencyList: [] as SelectOptionsView[]
    };
  },
  // 数据持久化
  persist: [
    {
      storage: sessionStorage
    }
  ],
  actions: {
    /**
     * @description:
     * @param key 需要存储的key名称
     * @param asyncFn 需要调用的API接口
     * @param params 接口调用所需参数
     * @return
     */
    async fetchDataIfNeeded(key: keyof SystemDictionaryView, asyncFn: (params: any) => Promise<any[]>, params?: any) {
      if (this.$state[key].length > 0) {
        return;
      }
      // 使用类型断言明确this的state类型
      await asyncFn(params).then((res) => {
        this.$state[key] = res;
      });
    }
  }
});

export default useCommonDictionaryStore;
